<html>
<head><meta charset="utf-8"><title>macro expansion inside cfg (a la extended-key-value-attrs) · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/macro.20expansion.20inside.20cfg.20(a.20la.20extended-key-value-attrs).html">macro expansion inside cfg (a la extended-key-value-attrs)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242157066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/macro%20expansion%20inside%20cfg%20%28a%20la%20extended-key-value-attrs%29/near/242157066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/macro.20expansion.20inside.20cfg.20(a.20la.20extended-key-value-attrs).html#242157066">(Jun 10 2021 at 03:56)</a>:</h4>
<p>Any chance we'll get the equivalent of extended-key-value-attributes for non-key-value cases (specifically, I'm interested with cfg)? I sometimes end up with complex <code>cfg</code> predicates, and would love to be able to factor them out. Concretely, I wanted to do something like this, but can't, and am wondering if this will be supported eventually</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">have_cycleclock</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">all</span><span class="p">(</span><span class="w"></span>
<span class="w">        </span><span class="n">not</span><span class="p">(</span><span class="n">miri</span><span class="p">),</span><span class="w"> </span><span class="c1">// miri &lt;/3 core::arch</span>
<span class="w">        </span><span class="n">feature</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"cycleclock"</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">any</span><span class="p">(</span><span class="n">target_arch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"x86"</span><span class="p">,</span><span class="w"> </span><span class="n">target_arch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"x86_64"</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="n">all</span><span class="p">(</span><span class="n">feature</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"unstable_asm"</span><span class="p">,</span><span class="w"> </span><span class="n">any</span><span class="p">(</span><span class="n">target_arch</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"..."</span><span class="p">,</span><span class="w"> </span><span class="o">..</span><span class="p">.)),</span><span class="w"></span>
<span class="w">     </span><span class="p">))</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="c1">// ... usage elsewhere...</span>
<span class="cp">#[cfg(have_cycleclock!())]</span><span class="w"></span>
<span class="k">mod</span> <span class="nn">cycleclock</span><span class="p">;</span><span class="w"></span>
<span class="c1">// and/or</span>
<span class="cp">#[cfg(not(have_cycleclock!()))]</span><span class="w"></span>
<span class="k">mod</span> <span class="nn">fallback</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>(note: i'm aware that hardware timestamp counters are not really the same as cycle clocks, but that's <em>extremely</em> not the point here, it's just example code of the kind of complex condition that might end up in several places in a library, which you could want to factor out)</p>



<a name="242183479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/macro%20expansion%20inside%20cfg%20%28a%20la%20extended-key-value-attrs%29/near/242183479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/macro.20expansion.20inside.20cfg.20(a.20la.20extended-key-value-attrs).html#242183479">(Jun 10 2021 at 10:03)</a>:</h4>
<p>I agree that being able to define cfg aliases would be quite welcome. At the very least, for the time being, you can use <a href="https://docs.rs/cfg_aliases">https://docs.rs/cfg_aliases</a> although it does involve defining the aliases in a <code>build.rs</code> script (although nothing prevents the aliases part from being defined in their own <code>cfg_aliases.rs</code> file).</p>
<p>For a slightly more cumbersome but inlineable workaround, using <a href="https://docs.rs/macro_rules_attribute">https://docs.rs/macro_rules_attribute</a> is another possibility:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span>::<span class="n">macro_rules_attribute</span>::<span class="n">macro_rules_attribute</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">apply</span><span class="p">;</span><span class="w"></span>

<span class="fm">macro_rules!</span><span class="w"> </span><span class="n">cfg_have_cycleclock</span><span class="w"> </span><span class="p">{(</span><span class="w"> </span><span class="cp">$item</span>:<span class="nc">item</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[cfg(all(</span>
<span class="cp">        not(miri), // miri &lt;/3 core::arch</span>
<span class="cp">        feature = </span><span class="s">"cycleclock"</span><span class="cp">,</span>
<span class="cp">        any(target_arch = </span><span class="s">"x86"</span><span class="cp">, target_arch = </span><span class="s">"x86_64"</span><span class="cp">),</span>
<span class="cp">        all(feature = </span><span class="s">"unstable_asm"</span><span class="cp">, any(target_arch = </span><span class="s">"..."</span><span class="cp">, ...)),</span>
<span class="cp">    ))]</span><span class="w"></span>
<span class="w">    </span><span class="cp">$item</span><span class="w"></span>
<span class="p">)}</span><span class="w"></span>

<span class="cp">#[apply(cfg_have_cycleclock!)]</span><span class="w"></span>
<span class="k">mod</span> <span class="nn">cycleclock</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="242198606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/macro%20expansion%20inside%20cfg%20%28a%20la%20extended-key-value-attrs%29/near/242198606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/macro.20expansion.20inside.20cfg.20(a.20la.20extended-key-value-attrs).html#242198606">(Jun 10 2021 at 12:36)</a>:</h4>
<p>Yeah, I'm aware <a href="http://build.rs">build.rs</a> can help here, but I avoid it for trivial stuff in libraries since it apparently can cause problems for people not using cargo as the build system. I've even had people in that situation tell me this is why they can't use libraries of mine in the past (rusqlite in particular) — I view it kinda like avoiding proc macros for to make build slightly easier on folks using static/musl systems — untenable for anything big, but nice if you can manage it for small stuff¹.</p>
<p>Using macro_rules_attribute for this is clever, but doesn't easily allow you to use it in cases where you negate the condition/use it with <code>any</code>, ... which is actually needed often IME.</p>
<hr>
<p>¹ Similarly, I don't really care enough about these weirder use cases to actually test on them — just enough to not deliberately break them if I can avoid it. Also, all other things being equal, builds that do not include a <a href="http://build.rs">build.rs</a> (or a proc macro) will be faster than ones that do, and reasoning about security is slightly easier if you don't run arbitrary code at build-time.</p>
<p>It also helps that I can usually assume that the end result of me doing something like this via <a href="http://build.rs">build.rs</a> would end up taking more code/time spent than it would save, although perhaps this isn't true if <code>cfg_aliases</code> is used (which I wasn't aware of, so thanks for the reference), and also likely isn't true for the proc macro case.</p>



<a name="242205566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/macro%20expansion%20inside%20cfg%20%28a%20la%20extended-key-value-attrs%29/near/242205566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/macro.20expansion.20inside.20cfg.20(a.20la.20extended-key-value-attrs).html#242205566">(Jun 10 2021 at 13:30)</a>:</h4>
<p>I'd also like ABI aliases, but that's probably a whole other discussion</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>